package com.zkingsoft.wechat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.web.BaseAction;
import com.zkingsoft.bean.BizBusiness;
import com.zkingsoft.bean.BizUser;
import com.zkingsoft.dao.BizBusinessDao;
import com.zkingsoft.redis.RedisUserLoginUtils;
import com.zkingsoft.service.BizBusinessService;

/**
 * @description 商铺相关接口
 * @author 姜友瑶
 * @date 2018-05-23 00:08
 */
@Controller
@RequestMapping(value = "wxapi/business")
public class WxBusinessAction extends BaseAction {

	@Autowired
	private BizBusinessService businessService;
	@Autowired
	private BizBusinessDao businessDao;

	@Autowired
	RedisUserLoginUtils redisUserLoginUtils;

	/**
	 * 新增商家
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年7月16日
	 */
	@RequestMapping(value = "/register")
	public @ResponseBody AjaxResult register(@RequestBody BizBusiness bizBusiness) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		bizBusiness.setOpenId(loginUser.getOpenId());
		bizBusiness.setCreateBy(loginUser.getNickName());
		bizBusiness.setUpdateBy(loginUser.getNickName());
		bizBusiness.setBusId(UUIDUtil.getRandomID());
		int i = businessDao.insert(bizBusiness);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "商户");
		} else {
			throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
		}
	}

	/**
	 * 更新商家
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年9月6日
	 * @param bizBusiness
	 * @return
	 */
	@RequestMapping(value = "/modify")
	public @ResponseBody AjaxResult modify(@RequestBody BizBusiness bizBusiness) {
		bizBusiness.setOpenId(redisUserLoginUtils.getLoginUser().getOpenId());
		int i = businessService.modifyByModel(bizBusiness);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, "商户");
		} else {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL);
		}
	}

	/**
	 * 查询商户没有分页
	 * 
	 * @author JIANGYOUYAO
	 * @email 935090232@qq.com
	 * @date 2018年9月6日
	 * @param openId
	 * @return
	 */
	@RequestMapping(value = "/findBusiness")
	public @ResponseBody AjaxResult findByOpenId(@RequestBody BizBusiness bizBusiness) {
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, businessService.findByModel(bizBusiness));
		return result;
	}

}